/**
 *  This file is part of PennerBot
 *
 *  Copyright (C) 2010 microkernel
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <core/messages.hpp>
#include <core/updatecheck.hpp>
#include <core/version.hpp>

namespace PBot
{
namespace Core
{

UpdateCheck::UpdateCheck(unsigned int id)
 : listener(P_StatusListener::GetInstance())
{
    ucConfig = new Config;

    ucConfig->id = id;
    ucConfig->botname = "UpdateCheck";
    ucConfig->website = "http://projecthost.ath.cx/micro/versions/versions2.txt";
    ucConfig->game = Config::UPDATECHECK;

    con = new Connection(ucConfig, listener);
    con->Init();
}

UpdateCheck::~UpdateCheck()
{
    delete con;
    delete ucConfig;
}

UpdateCheck::Version UpdateCheck::GetVersionStatus()
{
    if(IsAwake())
    {
        if(!con->Get(ucConfig->website, false))
        {
            listener.Dispatch(ucConfig->id, MSG_UC_ERROR);
            Sleep(60*15);
            return UpdateCheck::ERR;
        }

        std::string line;

        std::stringstream stream(con->GetBuffer());

        int versions = 0;

        while(std::getline(stream, line))
        {
            if(!line.empty())
            {
                std::string curVersion, safe;

                int pos = line.find(' ');
                curVersion = line.substr(0, pos);
                safe = line.substr(pos+1, line.size());

                if(curVersion == __PBOT_CORE_VERSION__ && versions == 0 && safe == "1")
                {
                    listener.Dispatch(ucConfig->id, MSG_UC_UPTODATE);
                    Sleep(60*15);
                    return UpdateCheck::UPTODATE;
                }
                else if(curVersion == __PBOT_CORE_VERSION__ && safe == "0")
                {
                    listener.Dispatch(ucConfig->id, MSG_UC_UNSAFE);
                    Sleep(60*15);
                    return UpdateCheck::UNSAFE;
                }
                else if(curVersion == __PBOT_CORE_VERSION__ && versions != 0)
                {
                    listener.Dispatch(ucConfig->id, MSG_UC_OLD);
                    Sleep(60*15);
                    return UpdateCheck::OLD;
                }

                ++versions;
            }
        }

        if(versions > 1)
        {
            listener.Dispatch(ucConfig->id, MSG_UC_UNKNOWN);
            Sleep(60*15);
            return UpdateCheck::UNKNOWN;
        }

        listener.Dispatch(ucConfig->id, MSG_UC_ERROR);
        Sleep(60*15);
        return UpdateCheck::ERR;

    }

    return UpdateCheck::ASLEEP;
}

Config* UpdateCheck::GetConfig()
{
    return ucConfig;
}

}
}
